<script setup>
import { RouterView } from 'vue-router'
import { ref, provide, onMounted } from 'vue'
import PUBGHeader from './components/PUBGHeader.vue'
import { authService } from './services/auth.js'

// 创建全局用户状态
const currentUser = ref(null)
const isLoading = ref(false)

// 加载用户信息
const loadUser = () => {
  isLoading.value = true
  try {
    const user = authService.getCurrentUser()
    if (user) {
      currentUser.value = user
    }
  } catch (error) {
    console.error('加载用户信息失败:', error)
  } finally {
    isLoading.value = false
  }
}

// 处理登出
const handleLogout = () => {
  authService.logout()
  currentUser.value = null
}

// 提供全局用户状态和方法
provide('currentUser', currentUser)
provide('isLoading', isLoading)
provide('loadUser', loadUser)
provide('handleLogout', handleLogout)

// 组件挂载时加载用户信息
onMounted(() => {
  loadUser()
})
</script>

<template>
  <header>
    <PUBGHeader />
  </header>
  <main>
    <RouterView />
  </main>
  <footer>
    <div class="footer-content">
      <p>&copy; 2024 和平精英攻略站. 保留所有权利.</p>
    </div>
  </footer>
</template>

<style>
#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  width: 100%;
}

header {
  width: 100%;
}

main {
  flex: 1;
  width: 100%;
}

footer {
  background-color: var(--color-background);
  padding: 1.5rem 0;
  border-top: 1px solid var(--color-border);
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  text-align: center;
  color: var(--color-text);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  line-height: 1.6;
  color: #333;
  background-color: #f5f5f5;
}

a {
  text-decoration: none;
  color: inherit;
}

ul, ol {
  list-style: none;
}

img {
  max-width: 100%;
  height: auto;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

@media (max-width: 768px) {
  .container {
    padding: 0 15px;
  }
}
</style>
